package io.agora.openduo.activities;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;

import java.util.List;

import io.agora.openduo.MyApp;
import io.agora.openduo.agora.Config;
import io.agora.openduo.utils.LogUtil;
import io.agora.rtm.ErrorInfo;
import io.agora.rtm.LocalInvitation;
import io.agora.rtm.RemoteInvitation;
import io.agora.rtm.ResultCallback;
import io.agora.rtm.RtmCallManager;
import io.agora.rtm.RtmChannelAttribute;
import io.agora.rtm.RtmChannelListener;
import io.agora.rtm.RtmChannelMember;
import io.agora.rtm.RtmFileMessage;
import io.agora.rtm.RtmImageMessage;
import io.agora.rtm.RtmMessage;

public abstract class AgoraBaseCallActivity extends AgoraBaseRtcActivity implements RtmChannelListener, ResultCallback<Void> {
    private static final String TAG = AgoraBaseCallActivity.class.getSimpleName();

    protected RtmCallManager mRtmCallManager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRtmCallManager = rtmCallManager();
    }

    public void gotoCallingInterface(String peerUid, String channel, int role) {
        gotoCallingActivity(channel, peerUid, role);
    }

    @Override
    public void onFileMessageReceived(RtmFileMessage rtmFileMessage, RtmChannelMember rtmChannelMember) {

    }

    @Override
    public void onImageMessageReceived(RtmImageMessage rtmImageMessage, RtmChannelMember rtmChannelMember) {

    }

    protected void gotoCallingActivity(String channel, String peer, int role) {
        Intent intent = new Intent(this, CallActivityAgora.class);
        intent.putExtra(Config.KEY_CALLING_CHANNEL, channel);
        intent.putExtra(Config.KEY_CALLING_PEER, peer);
        intent.putExtra(Config.KEY_CALLING_ROLE, role);
        startActivity(intent);
    }

    /**
     * 发送呼叫请求
     *
     * @param peerUid
     * @param channel
     */
    protected void inviteCall(final String peerUid, final String channel) {
        LocalInvitation invitation = mRtmCallManager.createLocalInvitation(peerUid);
        invitation.setContent(channel);
        mRtmCallManager.sendLocalInvitation(invitation, this);
        global().setLocalInvitation(invitation);
    }


    /**
     * 接受呼叫请求
     *
     * @param invitation
     */
    protected void answerCall(final RemoteInvitation invitation) {
        if (mRtmCallManager != null && invitation != null) {
            mRtmCallManager.acceptRemoteInvitation(invitation, this);
        }
    }

    protected void cancelLocalInvitation() {
        if (mRtmCallManager != null && global().getLocalInvitation() != null) {
            mRtmCallManager.cancelLocalInvitation(global().getLocalInvitation(), this);
        }
    }

    protected void refuseRemoteInvitation(@NonNull RemoteInvitation invitation) {
        if (mRtmCallManager != null) {
            mRtmCallManager.refuseRemoteInvitation(invitation, this);
        }
    }

    @Override
    public void onMemberCountUpdated(int count) {

    }

    @Override
    public void onAttributesUpdated(List<RtmChannelAttribute> list) {

    }

    @Override
    public void onMessageReceived(RtmMessage rtmMessage, RtmChannelMember rtmChannelMember) {

    }

    @Override
    public void onMemberJoined(RtmChannelMember rtmChannelMember) {

    }

    @Override
    public void onMemberLeft(RtmChannelMember rtmChannelMember) {

    }

    @Override
    public void onSuccess(Void aVoid) {

    }

    @Override
    public void onFailure(ErrorInfo errorInfo) {

    }

    @Override
    public void onLocalInvitationReceived(LocalInvitation localInvitation) {
        super.onLocalInvitationReceived(localInvitation);
    }

    @Override
    public void onLocalInvitationAccepted(LocalInvitation localInvitation, String response) {
        LogUtil.e(TAG, "onLocalInvitationAccepted by peer:" + localInvitation.getCalleeId());
        gotoVideoActivity(localInvitation.getContent(), localInvitation.getCalleeId());
    }

    @Override
    public void onLocalInvitationRefused(LocalInvitation localInvitation, String response) {
        super.onLocalInvitationRefused(localInvitation, response);
    }

    @Override
    public void onLocalInvitationCanceled(LocalInvitation localInvitation) {
        super.onLocalInvitationCanceled(localInvitation);
    }

    @Override
    public void onLocalInvitationFailure(LocalInvitation localInvitation, int errorCode) {
        super.onLocalInvitationFailure(localInvitation, errorCode);
        LogUtil.e(TAG, "onLocalInvitationFailure:" + errorCode);
    }

    @Override
    public void onRemoteInvitationReceived(RemoteInvitation remoteInvitation) {
        LogUtil.e(TAG, "onRemoteInvitationReceived from caller:" + remoteInvitation.getCallerId());
        if (MyApp.mConfig.isDev) {
            if (mRtmCallManager != null && remoteInvitation != null) {
                mRtmCallManager.acceptRemoteInvitation(remoteInvitation, this);
            }
        } else {
            global().setRemoteInvitation(remoteInvitation);
            gotoCallingActivity(remoteInvitation.getContent(), remoteInvitation.getCallerId(), Config.ROLE_CALLEE);
        }
    }

    @Override
    public void onRemoteInvitationAccepted(RemoteInvitation remoteInvitation) {
        LogUtil.e(TAG, "onRemoteInvitationAccepted from caller:" + remoteInvitation.getCallerId());
        gotoVideoActivity(remoteInvitation.getContent(), remoteInvitation.getCallerId());
    }

    @Override
    public void onRemoteInvitationRefused(RemoteInvitation remoteInvitation) {
        super.onRemoteInvitationRefused(remoteInvitation);
    }

    @Override
    public void onRemoteInvitationCanceled(RemoteInvitation remoteInvitation) {
        super.onRemoteInvitationCanceled(remoteInvitation);
    }

    @Override
    public void onRemoteInvitationFailure(RemoteInvitation remoteInvitation, int errorCode) {
        super.onRemoteInvitationFailure(remoteInvitation, errorCode);
        LogUtil.e(TAG, "onRemoteInvitationFailure:" + errorCode);
    }

    public void gotoVideoActivity(String channel, String peer) {
        Intent intent = new Intent(this, VideoActivityAgora.class);
        intent.putExtra(Config.KEY_CALLING_CHANNEL, channel);
        intent.putExtra(Config.KEY_CALLING_PEER, peer);
        startActivity(intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
